<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加赛区信息' : '编辑赛区信息'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="140px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="赛区编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入赛区编码" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="赛区名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入赛区名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getMatchAreaInfo, saveMatchAreaInfo } from "@/api/matchArea";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        code: "",
        name: "",
        orderNum: "",
      },
      rules: {
        // 表单规则
        code: [{ required: true, message: "请输入赛区编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入赛区名称", trigger: "blur" }],
        orderNum: [
          { required: true, message: "请输入排序号", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getMatchAreaInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveMatchAreaInfo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
